<?php
/**
 * Created by IntelliJ IDEA.
 * User: xingshi
 * Date: 16/8/30
 * Time: 下午11:16
 */

namespace App\Http\Controllers;


use App\Http\Requests;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;
use Crypt;
use App\Resume;
use App\Eduexp;
use App\Workexp;
use App\Invitation;
use App\Task;
use App\Job;
use Validator;
use App\User;
use App\Techlead;
use App\Utils;
use App\Permission;

class techleadController extends Controller
{
    static $URL = '/';
    public function __construct()
    {
        //          $this->middleware('auth');
    }

    public function index(Request $request){
		if (!Auth::check()) return;
        if (!Permission::user_is_super()) return;
        $invits = Invitation::whereRaw("(accepted=1 or hastouch=1) and isdelete=0")->get();
        $data = array();
        foreach($invits as $invit){
            $da = array();

            $jobid = $invit->jobid;
            $userid = $invit->userid;
            $job = Job::whereRaw("id = ?", [$jobid])->get()[0];
            $taskid = $job->taskid;
            $techleadid = $job->techleadid;
            $techleads = Techlead::whereRaw("id = ? and isdelete=0", [$techleadid])->get();
			if(count($techleads)>0) $techlead = $techleads[0];
			else continue;

			if($taskid==NULL) continue;
            $updated_at = DB::select("select updated_at from solves where taskid =".$taskid)[0]->updated_at;
			$da["invit_time"] = substr($updated_at, 0, 13);
            $da["accepted_time"] = substr($invit->updated_at, 0, 13);
            $da["title"] = DB::select("select title from tasks where id =".$taskid)[0]->title;
            $da["techlead_realname"] = $techlead->realname;
            $da["techlead_department"] = $techlead->department;
            $company = DB::select("select * from companies where id =".$techlead->companyid)[0];
            $da["techlead_company"] = $company->name;
            $da["user_name"] = User::whereRaw("id = ?", [$userid])->get()[0]->name;
            $da["job"] = $job->job;
            $da["salary"] = $job->salary;
            $da["acc_type"] = $invit->acc_type;
			$resumes = Resume::whereRaw("userid = ?", [$userid])->get(); 
            if(count($resumes)>0) $da["path"] = $resumes[0]->attachment_path;
			else $da["path"] = "";
            $da["u"] = Utils::id2url($userid);
            $da["r"] = Utils::id2url($techlead->userid);
			$da["accepted"] = $invit->accepted;
			$da["hastouch"] = $invit->hastouch;

            $data[] = $da;
        }
        return View::make("techlead.index")->with(compact("data"));

    }

    public function preview(Request $request){
        $userid = Auth::user()->id;
        $resumes = Resume::whereRaw("userid = ?", [$userid])->get();
        $resume = NULL;
        foreach($resumes as $k=>$v){
            $resume = $v;
        }
        $eduexps = Eduexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $workexps = Workexp::whereRaw("userid = ? and isdelete = 0", [$userid])->get();
        $user = User::find($userid);

        return View::make("resume.preview")->with(compact("user"))->with(compact("resume"))->with(compact("eduexps"))->with(compact("workexps"));
    }

}
